<?php

namespace App\Http\Controllers\zjxhome;

use App\Model\course;
use App\Model\infomation;
use App\Model\special;
use App\Model\writer;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;


use App\Http\Controllers\Controller;
class IndexController extends Controller
{
    //跳转到首页
    public function index()
    {
        $user = DB::table('courses')->paginate(2);
        $consult = DB::table('infomations')->paginate(3);
        return view('home-zjx.index',compact('user','consult'));
    }
    //跳转到课程
    public function course()
    {
        $course = course::all();
        return view('home-zjx.course',compact('course'));
    }
    //跳转到专栏
    public function special()
    {
        $special = special::all();
        return view('home-zjx.special',compact('special'));
    }
    //跳转到咨询
    public function consult()
    {
        $consult = infomation::all();
        return view('home-zjx.consult',compact('consult'));
    }

    //跳转到登陆界面
    public function login()
    {
        //
        return view('home-zjx.login');
    }
    //登陆验证
    public function doLogin(Request $request)
    {
        //定义验证规则
//        $rules = array(
//            'email' => 'required',
//            'password' => 'required',
//            'nickname' => 'required',
//        );
//
//        $message = array(
//            'email.required' => '邮箱账号不能为空',
//            'password.required' =>  '密码不能为空',
//            'nickname' => '用户名不能为空',
//        );
//        $this->validate($request,$rules,$message);

        //获取表单的提交数据
            $email = $request->input('email');
            if (!$email){
                $request->session()->flash('zjxemail', '1');
                return back();
            }
            $password = $request->input('password');
        if (!$password){
            $request->session()->flash('zjxpassword', '1');
            return back();
        }
            $nickname = $request->input('nickname');
            $users=writer::where('email',$email)->where('nickname',$nickname)->where('password',$password)->get();

            $consult = infomation::paginate(2);
            $user=course::paginate(2);
            foreach ($users as $k){
                $id=$k->id;
            }
//            dd($user);
            if(!count($users)){
//                dd(33);
                echo "<script> alert('填写信息有误!');javascript:history.go(-1);</script>";
            }else{
//                dd(22);
                session(['nickname'=>$request->nickname]);
                session(['writerid'=>$id]);
                return view('home-zjx.index',compact('user','consult','user'));
            }

    }

    //跳转到注册
    public function register()
    {
        return view('home-zjx.register');
    }

    //保存用户信息
    public function store(Request $request)
    {
        if ($request->isMethod('post')){
            $rules = array(
                'nickname'      =>      'required|unique:readusers,nickname',
                'password'      =>      'required|confirmed',
                'password_confirmation'     =>      'required',
                'email'         =>       'required|email',
            );

            $mess = array(
                'nickname.required'   =>    '用户名不能为空',
                'nickname.unique'     =>    '用户名已被注册',
                'password.required'   =>    '密码不能为空',
                'password.confirmed'  =>    '俩次密码不一致',
                'password_confirmation.required' => '确认密码不能为空',
                'email.required'               =>    '邮箱不能为空',
            );

            $this->validate($request,$rules,$mess);
            $add=new writer();
            $add->email=$request->email;
            $add->password=$request->password;
            $add->nickname=$request->nickname;
            $add->save();

            if($add){
               return view('home-zjx.login');
            }
        }
    }

    //去上课
    public function go_course($id,$do)
    {
        if ($do=='a'){
            $special = special::find($id);
        }elseif($do=='c'){
            $consult = infomation::find($id);
        }else{
            $go_course = course::find($id);
        }
        return view('home-zjx.go_course',compact('go_course','special','consult'));
    }
}
